package com.tanhua.server.controller;

import cn.hutool.core.date.SystemClock;
import com.tanhua.model.domain.BaseResult;
import com.tanhua.server.service.UserService;
import lombok.extern.log4j.Log4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping("/user")
@RefreshScope
public class LoginController {

    @Value("${customuser.name}")
    String name;


    @GetMapping("/config")
    public ResponseEntity config(){
        return ResponseEntity.ok(name);
    }

    @Autowired
    UserService userService;

    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        userService.sendMsg(phone);
        LogFactory.getLog(this.getClass()).debug("获取验证码方法调用");
        //延迟两s
        return ResponseEntity.ok(BaseResult.builder().data("").build());
    }

    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        String code = (String) map.get("verificationCode");
        LogFactory.getLog(this.getClass()).debug("loginVerification方法调用");
        Map result = userService.loginVerification(phone, code);
        return ResponseEntity.ok(BaseResult.builder().data(result).build());
    }
}
